<?php

namespace app\common\business;

use app\common\model\WardmateCircleContent;
use catcher\CatchUpload;
use catcher\Utils;
use think\File;
use think\Image;
use app\common\utils\Upfile;
use catcher\exceptions\FailedException;
use app\common\model\WardmateCircle as WcircleModel;
use app\common\model\WardmateCircleContent as WContentModel;

class Circle
{
    protected $model = null;

    public function __construct()
    {
        $this->model = new WardmateCircleContent();
    }

    /**
     * 获取群聊头部信息
     * @param string $id 群聊主键
     * @return array
     */
    public function getHeadinfo(string $id): array
    {
        $info = (new WcircleModel())->getOneData(['id' => $id], "title,patientList");

        if (empty($info)) {
            throw new FailedException("群聊不存在");
        }

        //群聊人数,医生+1
        $peopleNum = isset($info['patientList']) || !empty($info['patientList']) ? count($info['patientList']) + 1 : 1;
        $data['title'] = $info['title'] . "(" . $peopleNum . "人)";
        return $data;
    }

    public function uploadFiles($files): array
    {
        $rnd = [];
        $thumbPath = Utils::publicPath() . "/files/circleOfFriends/" . date("Ymd");
        $result = (new CatchUpload())->setPath("circleOfFriends")->upload($files);
        $rnd["path"] = $rnd["thumb_path"] = "";
        if (!empty($result)) {
            //执行缩略图
            $rnd["path"] = $result;
            if ($files->getMime() !== "video/mp4") {
                $arr = explode("/", $result);
                $image = Image::open("./" . $result);
                [$fileName, $extension] = explode(".", $arr[count($arr) - 1]);
                $thumbUrl = $thumbPath . "/" . $fileName . "_thumb." . $extension;
                $result = $image->thumb(200, 200)->save($thumbUrl);
                if (file_exists($thumbUrl)) {
                    $rnd["thumb_path"] = "/files/circleOfFriends/" . date("Ymd") .'/'. $fileName . "_thumb." . $extension;
                } else {
                    $rnd["thumb_path"] = $rnd["path"];
                }
            }
        }
        return $rnd;
    }

    /**
     * 发布朋友圈
     * @param array $data
     */
    public function release(array $data)
    {
        return $this->model->createBy($data);

    }



    /**
     * 发布内容
     * @param array $data 参数
     * @return bool
     */
    // public function releaseCotent(array $data=[]): bool
    // {
    //     halt($data['info']);
    //     //医生发布
    //     if ($data['type'] == 1)
    //     {
    //         $field = "doctor_code";
    //         $code = $data['doctor_code'];
    //     }else{
    //         $field = "patientList.patientId";
    //         $code = $data['patientId'];
    //     }     
    //     $contentType = isset($data['contentType'])&&!empty($data['contentType'])?$data['contentType']:1;
    //     $written = isset($data['written'])&&!empty($data['written'])?$data['written']:"";
    //     $info = isset($data['info'])&&!empty($data['info'])?$data['info']:[];
    //     return  $this->contentHandle($data['id'], $data['type'], $field, $code, $contentType, $written, $info);
    // }

    /**
     * 发布内容处理
     * @param string $id 群聊主键ID
     * @param int $type 发布人类型：1 医生，2 患者
     * @param string $field 发布人字段
     * @param string $code 医生doctor_code|患者patientId
     * @param int $contentType 内容类型：1 文字 2 图片，3 视频
     * @param string $written 文字
     * @param array $info 图片信息/视频信息
     * @return bool
     */
    // private function contentHandle(string $id, int $type, string $field, string $code, int $contentType=1, string $written="", array $info=[]): bool
    // {
    //     try {
    //         $is_exist = (new WcircleModel())->getOneData(['id' => $id, $field => $code], "id");

    //         if (empty($is_exist))
    //         {
    //             throw new FailedException("您不在此群聊");
    //         }

    //         //判断内容类型
    //         // switch ($contentType)
    //         // {
    //         //     //图片
    //         //     case 2:
    //         //         $upData = (new Upfile())->upLoad($info, 1);
    //         //         break;
    //         //     //视频    
    //         //     case 3:
    //         //         $upData = (new Upfile())->upLoad($info, 2);
    //         //         break;   
    //         //     default:
    //         // }

    //         //判断医生||患者
    //         $type == 1 ? $data['doctor_code'] = $code:$data['patientId'] = $code;
    //         $data['circleId'] = $id; 
    //         $data['type'] = $type;
    //         $data['written'] = $written;
    //         $data['contentType'] = $contentType;
    //         $data['content'] = $info;
    //         $data['deleted_at'] = 0;
    //         return  (new WContentModel())->addData($data);
    //     } catch (\Exception $exception) {
    //         throw new FailedException($exception->getMessage());
    //     }    
    // }
}